package breeze.pixel.weather.share.presenter;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.io.File;

import breeze.pixel.weather.BaseView;
import breeze.pixel.weather.R;
import breeze.pixel.weather.apps_util.utils.AppToolUtil;
import breeze.pixel.weather.apps_util.utils.appsettings.AppSettings;
import breeze.pixel.weather.apps_util.views.mtextview.MTextView;
import breeze.pixel.weather.share.view.ShareWeatherView;

public class ShareWeatherPresenter implements IShareWeatherPresenter {

    private final ShareWeatherView view;

    public ShareWeatherPresenter(ShareWeatherView v) {
        this.view = v;
    }

    @Override
    public void layout(Bundle data, View v) {
        BaseView.getExec().execute(new Runnable() {
            @Override
            public void run() {
                AppSettings sets = AppSettings.getInstance(view);
                MTextView tv_cityName = v.findViewById(R.id.share_city_name);
                tv_cityName.setTextSize(25);
                MTextView tv_cityId = v.findViewById(R.id.share_city_id);
                tv_cityId.setTextSize(20);
                MTextView tv_tem = v.findViewById(R.id.share_tem);
                tv_tem.setTextSize(30);
                MTextView tv_wind = v.findViewById(R.id.share_wind);
                tv_wind.setTextSize(20);
                MTextView tv_weather = v.findViewById(R.id.share_weather);
                tv_weather.setTextSize(25);
                MTextView tv_date = v.findViewById(R.id.share_date);
                tv_date.setTextSize(20);
                MTextView tv_wet = v.findViewById(R.id.share_wet);
                tv_wet.setTextSize(20);
                MTextView tv_time = v.findViewById(R.id.share_date_time);
                tv_time.setTextSize(20);
                ImageView img_icon = v.findViewById(R.id.share_weather_icon);
                ImageView img_background = v.findViewById(R.id.share_bg);
                LinearLayout logo_layout = v.findViewById(R.id.share_logo_linearLayout);
                if (sets.isDonated()){
                    logo_layout.setVisibility(View.GONE);
                }else {
                    logo_layout.setVisibility(View.VISIBLE);
                }
                tv_cityName.setText(data.getString("cityName"));
                tv_cityId.setText(data.getString("cityId"));
                tv_tem.setText(data.getString("tem"));
                tv_wind.setText(data.getString("wind"));
                tv_weather.setText(data.getString("weather"));
                tv_date.setText(AppToolUtil.getSimpleDate("yyyy/MM/dd"));
                tv_time.setText(AppToolUtil.getSimpleDate("HH:mm"));
                tv_wet.setText(data.getString("wet"));
                img_icon.setImageBitmap(AppToolUtil.getweatherIcon(view.getApplicationContext(), data.getString("icon")));
                switch (sets.getBackgroundModeType()) {
                    default:
                    case AppSettings.TYPE_BACKGROUNDMODE_DEFAULT:
                        Bitmap bitmap = AppToolUtil.getBingPic(view);
                        if (bitmap != null) {
                            img_background.setImageBitmap(bitmap);
                        }
                        break;
                    case AppSettings.TYPE_BACKGROUNDMODE_CUSTOM:
                        if (new File(AppToolUtil.getMainBackgroundPic(view)).exists()) {
                            String string = AppToolUtil.getMainBackgroundPic(view);
                            Bitmap bitmap1 = BitmapFactory.decodeFile(string);
                            if (bitmap1 != null) {
                                img_background.setImageBitmap(bitmap1);
                            }
                        } else {
                            Bitmap bitmap2 = AppToolUtil.getBingPic(view);
                            if (bitmap2 != null) {
                                img_background.setImageBitmap(bitmap2);
                            }
                        }
                        break;
                }
                DisplayMetrics metrics = new DisplayMetrics();
                view.getWindowManager().getDefaultDisplay().getMetrics(metrics);
                int width = metrics.widthPixels;
                int height = metrics.heightPixels;
                v.layout(0, 0, width, height);
                int mWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
                int mHeight = View.MeasureSpec.makeMeasureSpec(10000, View.MeasureSpec.AT_MOST);
                v.measure(mWidth, mHeight);
                v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
                int w = v.getWidth();
                int h = v.getHeight();
                Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
                Canvas c = new Canvas(bmp);
                c.drawColor(Color.WHITE);
                v.layout(0, 0, w, h);
                v.draw(c);
                view.setPicView(bmp);
            }
        });
    }
}
